import { loginAPI, getProfileAPI } from '@/api/user'
export default {
  namespaced: true,
  state() {
    return {
      token: '',
      profile: {}
    }
  },

  mutations: {
    setUserToken(state, token) {
      state.token = token
    },
    setProfile(state, profile) {
      console.log(profile)

      state.profile = profile
    },
    clearUserInfo(state) {
      // 清除Token
      state.token = ''
      state.profile = {}
    }

  },
  actions: {
    // 登录
    async handlelogin(context, obj) {
      const res = await loginAPI(obj)
      console.log(res)

      context.commit('setUserToken', res.data.token)
    },
    // 退出
    handlelogout() {
      this.$store.commit('user/setUserToken', '')
      this.$router.push('/login')
    },
    async getProfile(ctx) {
      const res = await getProfileAPI()
      ctx.commit('setProfile', res.data)
      return res.data.permissions
    }
  }
}
